package org.dromara.easytl.parser;

/**
 * Token 类型定义（使用 int 常量）
 * 
 * <p>使用 int 魔数表示 Token 类型，避免枚举的性能开销</p>
 *
 * @author EasyTL Team
 */
public final class TokenType {

    // ==================== 特殊 ====================
    public static final int EOF = 0;                // 文件结束
    public static final int TEXT = 1;               // 普通文本内容

    // ==================== 分隔符 ====================
    public static final int LEFT_BRACE = 10;        // {
    public static final int RIGHT_BRACE = 11;       // }
    public static final int LEFT_PAREN = 12;        // (
    public static final int RIGHT_PAREN = 13;       // )
    public static final int LEFT_BRACKET = 14;      // [
    public static final int RIGHT_BRACKET = 15;     // ]
    public static final int COMMA = 16;             // ,
    public static final int DOT = 17;               // .
    public static final int SEMICOLON = 18;         // ;
    public static final int BACKTICK = 19;          // `（字符串模板）
    public static final int SLASH = 20;             // /（正则表达式和除法共用）

    // ==================== 基本运算符 ====================
    public static final int PLUS = 30;              // +
    public static final int MINUS = 31;             // -
    public static final int MULTIPLY = 32;          // *
    public static final int DIVIDE = 33;            // /
    public static final int MODULO = 34;            // %
    public static final int GT = 35;                // >
    public static final int LT = 36;                // <
    public static final int GTE = 37;               // >=
    public static final int LTE = 38;               // <=
    public static final int EQ = 39;                // ==
    public static final int NEQ = 40;               // !=
    public static final int AND = 41;               // &&
    public static final int OR = 42;                // ||
    public static final int NOT = 43;               // !
    public static final int QUESTION = 44;          // ?
    public static final int COLON = 45;             // :
    public static final int ASSIGN = 46;            // =

    // ==================== 高级运算符 ====================
    public static final int SAFE_NAV = 50;                      // ?. 空安全操作符
    public static final int ELVIS = 51;                         // ?: Elvis 运算符
    public static final int REGEX_MATCH = 52;                   // =~ 正则匹配
    public static final int REGEX_NOT_MATCH = 53;               // !=~ 正则不匹配
    
    // 区间运算符（四种类型）
    public static final int RANGE_CLOSED = 54;                  // .. 全闭区间 [a, b]
    public static final int RANGE_OPEN = 55;                    // >..< 全开区间 (a, b)
    public static final int RANGE_LEFT_CLOSED_RIGHT_OPEN = 56;  // ..< 前闭后开 [a, b)
    public static final int RANGE_LEFT_OPEN_RIGHT_CLOSED = 57;  // >.. 前开后闭 (a, b]

    // ==================== 字面量 ====================
    public static final int IDENTIFIER = 70;        // 标识符
    public static final int STRING = 71;            // 字符串字面量（单引号/双引号）
    public static final int TEMPLATE_STRING = 72;   // 字符串模板字面量（反引号）
    public static final int NUMBER = 73;            // 数值字面量 (int/double)
    public static final int LONG = 74;              // 长整数字面量（L 结尾）
    public static final int BIG_INTEGER = 75;       // 大整数字面量（超出 long 范围）
    public static final int BIG_DECIMAL = 76;       // 高精度小数字面量（BigDecimal）
    public static final int REGEX = 77;             // 正则表达式字面量（/pattern/flags）
    public static final int TRUE = 78;              // true
    public static final int FALSE = 79;             // false
    public static final int NULL = 80;              // null

    // ==================== 关键字 ====================
    public static final int IF = 90;                // if
    public static final int ELSE = 91;              // else
    public static final int FOR = 92;               // for
    public static final int IN = 93;                // in（范围检查、循环）
    public static final int NOT_IN = 94;            // !in（不包含判断）
    public static final int SWITCH = 95;            // switch
    public static final int CASE = 96;              // case
    public static final int DEFAULT = 97;           // default
    public static final int LET = 98;               // let（变量声明）
    public static final int EXTENDS = 99;           // extends（模板扩展）
    public static final int IMPORT = 100;           // import（Java 类导入）

    // ==================== 语句块标记 ====================
    /**
     * 语句块开始标记 {%
     * <p>用于标准语句块和自闭合语句块的开始</p>
     * <ul>
     *   <li>标准语句块：{% 语句名称 参数 } ... {/% 语句名称 }</li>
     *   <li>自闭合语句块：{% 语句 %}</li>
     * </ul>
     */
    public static final int STMT_BEGIN = 110;       // {%
    
    /**
     * 自闭合语句块结束标记 %}
     * <p>用于自闭合语句块的结束</p>
     */
    public static final int STMT_END = 111;         // %}
    
    /**
     * 标准语句块结束标签开始 {/%
     * <p>用于标准语句块的结束标签开始，后面跟语句名称和 }</p>
     */
    public static final int STMT_CLOSE = 112;       // {/%

    // ==================== 表达式标记 ====================
    /**
     * 单花括号表达式开始标记
     * <p>格式：{表达式}</p>
     */
    public static final int SINGLE_BRACE_EXPR_BEGIN = 120;  // {
    
    /**
     * 单花括号表达式结束标记
     * <p>格式：{表达式}</p>
     */
    public static final int SINGLE_BRACE_EXPR_END = 121;    // }
    
    /**
     * 双花括号表达式开始标记
     * <p>格式：{{表达式}}</p>
     */
    public static final int DOUBLE_BRACE_EXPR_BEGIN = 122;  // {{
    
    /**
     * 双花括号表达式结束标记
     * <p>格式：{{表达式}}</p>
     */
    public static final int DOUBLE_BRACE_EXPR_END = 123;    // }}
    
    /**
     * 美元符号表达式开始标记
     * <p>格式：${表达式}</p>
     */
    public static final int DOLLAR_BRACE_EXPR_BEGIN = 124;  // ${
    
    /**
     * 美元符号表达式结束标记
     * <p>格式：${表达式}</p>
     */
    public static final int DOLLAR_BRACE_EXPR_END = 125;    // }

    private TokenType() {
        // 工具类，禁止实例化
    }

    /**
     * 获取 Token 类型的名称（用于调试和日志输出）
     * 
     * @param type Token 类型常量
     * @return Token 类型名称字符串
     */
    public static String getName(int type) {
        switch (type) {
            // 特殊
            case EOF: return "EOF";
            case TEXT: return "TEXT";
            
            // 分隔符
            case LEFT_BRACE: return "LEFT_BRACE";
            case RIGHT_BRACE: return "RIGHT_BRACE";
            case LEFT_PAREN: return "LEFT_PAREN";
            case RIGHT_PAREN: return "RIGHT_PAREN";
            case LEFT_BRACKET: return "LEFT_BRACKET";
            case RIGHT_BRACKET: return "RIGHT_BRACKET";
            case COMMA: return "COMMA";
            case DOT: return "DOT";
            case SEMICOLON: return "SEMICOLON";
            case BACKTICK: return "BACKTICK";
            case SLASH: return "SLASH";
            
            // 基本运算符
            case PLUS: return "PLUS";
            case MINUS: return "MINUS";
            case MULTIPLY: return "MULTIPLY";
            case DIVIDE: return "DIVIDE";
            case MODULO: return "MODULO";
            case GT: return "GT";
            case LT: return "LT";
            case GTE: return "GTE";
            case LTE: return "LTE";
            case EQ: return "EQ";
            case NEQ: return "NEQ";
            case AND: return "AND";
            case OR: return "OR";
            case NOT: return "NOT";
            case QUESTION: return "QUESTION";
            case COLON: return "COLON";
            case ASSIGN: return "ASSIGN";
            
            // 高级运算符
            case SAFE_NAV: return "SAFE_NAV";
            case ELVIS: return "ELVIS";
            case REGEX_MATCH: return "REGEX_MATCH";
            case REGEX_NOT_MATCH: return "REGEX_NOT_MATCH";
            case RANGE_CLOSED: return "RANGE_CLOSED";
            case RANGE_OPEN: return "RANGE_OPEN";
            case RANGE_LEFT_CLOSED_RIGHT_OPEN: return "RANGE_LEFT_CLOSED_RIGHT_OPEN";
            case RANGE_LEFT_OPEN_RIGHT_CLOSED: return "RANGE_LEFT_OPEN_RIGHT_CLOSED";
            
            // 字面量
            case IDENTIFIER: return "IDENTIFIER";
            case STRING: return "STRING";
            case TEMPLATE_STRING: return "TEMPLATE_STRING";
            case NUMBER: return "NUMBER";
            case LONG: return "LONG";
            case BIG_INTEGER: return "BIG_INTEGER";
            case BIG_DECIMAL: return "BIG_DECIMAL";
            case REGEX: return "REGEX";
            case TRUE: return "TRUE";
            case FALSE: return "FALSE";
            case NULL: return "NULL";
            
            // 关键字
            case IF: return "IF";
            case ELSE: return "ELSE";
            case FOR: return "FOR";
            case IN: return "IN";
            case NOT_IN: return "NOT_IN";
            case SWITCH: return "SWITCH";
            case CASE: return "CASE";
            case DEFAULT: return "DEFAULT";
            case LET: return "LET";
            case EXTENDS: return "EXTENDS";
            case IMPORT: return "IMPORT";
            
            // 语句块标记
            case STMT_BEGIN: return "STMT_BEGIN";
            case STMT_END: return "STMT_END";
            case STMT_CLOSE: return "STMT_CLOSE";
            
            // 表达式标记
            case SINGLE_BRACE_EXPR_BEGIN: return "SINGLE_BRACE_EXPR_BEGIN";
            case SINGLE_BRACE_EXPR_END: return "SINGLE_BRACE_EXPR_END";
            case DOUBLE_BRACE_EXPR_BEGIN: return "DOUBLE_BRACE_EXPR_BEGIN";
            case DOUBLE_BRACE_EXPR_END: return "DOUBLE_BRACE_EXPR_END";
            case DOLLAR_BRACE_EXPR_BEGIN: return "DOLLAR_BRACE_EXPR_BEGIN";
            case DOLLAR_BRACE_EXPR_END: return "DOLLAR_BRACE_EXPR_END";
            
            default: return "UNKNOWN(" + type + ")";
        }
    }
}

